package com.helloworld.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.helloworld.dao.PurchaselistDao;
import com.helloworld.pojo.Purchaselist;


@Repository
public class PurchaselistDaoImpl extends HibernateDaoSupport implements PurchaselistDao {
	
	@Autowired
    public void setSessionFactoryOverride(SessionFactory sessionFactory)  
    {  
        super.setSessionFactory(sessionFactory);  
    } 

	@Override
	public List<Purchaselist> findByPage(String purchaseTime, Integer purchaseId,Integer invoiceNumber,String supplierName,String purchasePerson,Integer pageNum, Integer pageSize) {
		List<Purchaselist> list=(List<Purchaselist>) super.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(Purchaselist.class);
				if (purchaseTime!=null) {
					criteria.add(Restrictions.sqlRestriction("purchase_time like (?)","%"+purchaseTime+"%", StandardBasicTypes.STRING));
				}
			    if (purchaseId!=null) {
			    	criteria.add(Restrictions.sqlRestriction("purchase_id like (?)","%"+purchaseId+"%", StandardBasicTypes.STRING));
				}
			    if (invoiceNumber!=null) {
			    	criteria.add(Restrictions.sqlRestriction("invoice_number like (?)","%"+invoiceNumber+"%", StandardBasicTypes.STRING));
				}
			    if (supplierName!=null) {
			    	criteria.add(Restrictions.sqlRestriction("supplier_name like (?)","%"+supplierName+"%", StandardBasicTypes.STRING));
				}
			    if (purchasePerson!=null) {
			    	criteria.add(Restrictions.sqlRestriction("purchase_person like (?)","%"+purchasePerson+"%", StandardBasicTypes.STRING));
				}
			    
				int page=(pageNum-1)*pageSize;
				criteria.setFirstResult(page);
				criteria.setMaxResults(pageSize);
				return criteria.list();
			}
		});
		return list;
	}

	@Override
	public int addPurchase(Purchaselist purchaselist) {
		try {
			super.getHibernateTemplate().save(purchaselist);
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		}
		
	}

	@Override
	public List<Purchaselist> findAll() {
		List<Purchaselist> list = (List<Purchaselist>) super.getHibernateTemplate().find("from Purchaselist");
		return list;
	}


	@Override
	public List<Purchaselist> findByEntity(String time, Integer purchaseId,Integer invoiceNumber,String supplierName,String purchasePerson) {
		List<Purchaselist> list=(List<Purchaselist>) super.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(Purchaselist.class);
				if (time!=null) {
					criteria.add(Restrictions.sqlRestriction("purchase_time like (?)","%"+time+"%", StandardBasicTypes.STRING));
				}
			    if (purchaseId!=null) {
			    	criteria.add(Restrictions.sqlRestriction("purchase_id like (?)","%"+purchaseId+"%", StandardBasicTypes.STRING));
				}
			    if (invoiceNumber!=null) {
			    	criteria.add(Restrictions.sqlRestriction("invoice_number like (?)","%"+invoiceNumber+"%", StandardBasicTypes.STRING));
				}
			    if (supplierName!=null) {
			    	criteria.add(Restrictions.sqlRestriction("supplier_name like (?)","%"+supplierName+"%", StandardBasicTypes.STRING));
				}
			    if (purchasePerson!=null) {
			    	criteria.add(Restrictions.sqlRestriction("purchase_person like (?)","%"+purchasePerson+"%", StandardBasicTypes.STRING));
				}
				return criteria.list();
			}
		});
		return list;
	}

	@Override
	public int delPurchase(Integer purchaseId) {
		// TODO Auto-generated method stub
		Purchaselist purchaselist = new Purchaselist();
		purchaselist.setPurchaseId(purchaseId);
		try {
			super.getHibernateTemplate().delete(purchaselist);
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		}
	}


}
